import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-09
 * Time: 8:06
 */
//数组(2)补充+数组（1）+迭代递归的简单理解题
public class TestDemo {
    /*
    数组
     */
    //不规则数组
    public static void main(String[] args) {
        int[][] array = new int[2][];
        System.out.println(Arrays.toString(array));
        array[0] = new int[2];
        array[1] = new int[5];
        System.out.println(Arrays.deepToString(array));
    }
    //二维数组的创建和遍历
    public static void main10(String[] args) {
        int[][] array1 = {{1,2,3},{4,5,6}};
        int[][] array2 = new int[][]{{1,2,3},{4,5,6}};
        int[][] array3 = new int[2][3];

        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j]+" ");
            }
        }
        System.out.println();
        System.out.println("=========================");


        for (int[] tmp: array1) {
            for (int x :tmp) {
                System.out.print(x+" ");
            }
        }
        System.out.println();
        System.out.println("=========================");

        System.out.println(Arrays.deepToString(array1));
    }
    //数组只要还在被引用就不会释放
    public static int[] func(){
        int[] array = {1,2,3,4};
        return array;
    }
    public static void main9(String[] args) {
        int[] array = func();
        System.out.println(Arrays.toString(array));
    }
    //数组是一个引用类型
    public static void func1(int[] array1){
        array1 = new int[]{9,8,7};
    }
    public static void func2(int[] array2){
        array2[0] = 99;
    }
    public static void main8(String[] args) {
        int[] array = {1,2,3,4};
        func1(array);
        System.out.println(Arrays.toString(array));
        func2(array);
        System.out.println(Arrays.toString(array));
    }
    //数组的创建和遍历
    public static void main1(String[] args) {
        int[] array1 = {1,2,3,4};
        int[] array2 = new int[] {1,2,3,4};
        int[] array3 = new int[4];
        for (int i = 0; i < array1.length; i++) {
            System.out.print(array1[i]);
        }

        for (int x:
                array2) {
            System.out.println(x);
        }

        System.out.println(Arrays.toString(array3));
    }
    /*
    递归，迭代的简单了解
     */
    //递归求 1 + 2 + 3 + ... + 10
    public static int sum(int n){
        if(n == 1){
            return 1;
        }else{
            int sum = 0;
            sum = n + sum(n-1);
            return sum;
        }
    }
    public static void main7(String[] args) {
        //终止条件  归到1停止
        //递归公式  sum(n) = n + sum(9)
        int ret = sum(10);
        System.out.println(ret);
    }
    //按顺序打印一个数字的每一位(例如 1234 打印出 1 2 3 4) （递归）
    public static void print(int n){
        if(n < 10){
            System.out.println(n);
        }else{
            int tmp = n % 10;
            print(n/10);
            System.out.println(tmp);
        }
    }
    public static void main6(String[] args) {
        //终止条件  归到个位停止
        //递归公式  n%10 /10 -> n<10
        print(123);
    }
    //递归求斐波那契数列的第 N 项
    public static int fib2(int n){
        if(n == 1 || n == 2){
            return 1;
        }else{
            int tmp = fib2(n-1)+fib2(n-2);
            return tmp;
        }
    }
    public static void main5(String[] args) {
        //终止条件   归到第1项或第2项
        //递归公式   fib(n) = fib(n-1) + fib(n-2)
        Scanner n = new Scanner(System.in);
        int ret = fib2(n.nextInt());
        System.out.println(ret);
    }
    //写一个递归方法，输入一个非负整数，返回组成它的数字之和
    public static int sumEvery(int n){
        //终止条件  个位数时停止递归
        //递推公式  n = n%10 + (n/10)%10 + ((n/10)%10)%10;
        if(n < 10){
            return n;
        }else{
            int tmp = n%10 + sumEvery(n/10);
            return tmp;
        }
    }
    public static void main4(String[] args) {
        int ret = sumEvery(123);
        System.out.println(ret);
    }
    //求斐波那契数列的第n项。(迭代实现) 1 1 2 3 5
    public static int fib1(int n){
        if(n <= 0){
            return -1;
        } else if(n == 1 || n == 2){
            return 1;
        }else {
            int n1 = 1;
            int n2 = 1;
            int n3 = 0;
            for (int i = 3; i <= n; i++) {
                n3 = n1 + n2;
                n1 = n2;
                n2 = n3;
            }
            return n3;
        }
    }
    public static void main3(String[] args) {
        int ret = fib1(8);
        System.out.println(ret);
    }
    //n！
    public static int fac(int n){
        //终止条件
        //递推公式
        if(n == 1){
            return 1;
        }
        int tmp = n * fac(n - 1);
        return tmp;
    }
    public static void main2(String[] args) {
        Scanner n = new Scanner(System.in);
        int ret = fac(n.nextInt());
        System.out.println(ret);
    }
}
